import sys
import math
import typing
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets


class EnemyBullet(QGraphicsItem):
    def __init__(self, scene):
        super(EnemyBullet, self).__init__()
        self.root = QFileInfo(__file__).absolutePath()

        self.setVisible(False)  # 设为不可视
        self.speed = 1
        self.Rotate = 0
        self.blood = 10000
        self.current_blood = 10000

        self.pix = []
        for i in range(3):
            self.pix.append(QPixmap(self.root + "/res/big_bad_bullet_0" + str(i + 1) + ".png"))

        self.state = 0

        self.stopTime = 0

        self.scene = scene
        self.scene.addItem(self)

        self.MuchSet = 0  # 表示第几集合

    def setSet(self, MuchSet):
        self.MuchSet = MuchSet

    def boundingRect(self):
        return QRectF(-self.pix[0].width() * 0.5, -self.pix[0].height() * 0.5, self.pix[0].width(),
                      self.pix[0].height() - 16)

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...):
        painter.drawPixmap(-self.pix[self.state].width() * 0.5, -self.pix[self.state].height() * 0.5,
                           self.pix[self.state])
        self.state = (self.state + 1) % 3

    def advance(self, phase: int):
        self.stopTime = (self.stopTime + 1) % 20  # 延迟进行
        if self.stopTime % 10 == 0:
            if self.isVisible():
                if self.mapToScene(0, 0).x() < 0 or self.mapToScene(0, 0).y() < 0 or \
                        self.mapToScene(0, 0).x() > self.scene.width() or self.mapToScene(0,
                                                                                          0).y() > self.scene.height():
                    self.setVisible(False)
                else:
                    self.setPos(self.mapToScene(self.speed, 0))

    def setBlood(self, damage):
        pass

    def CallSmoke(self):
        pass

    def setPos(self, pos: typing.Union[QPointF, QPoint]):
        super(EnemyBullet, self).setPos(pos)

    def setRotation(self, angle: float):
        self.Rotate = angle
        super(EnemyBullet, self).setRotation(angle)

    def setVisible(self, visible: bool):
        super(EnemyBullet, self).setVisible(visible)

    def isVisible(self):
        return super(EnemyBullet, self).isVisible()
